﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdCloseFile.h"
#include "CommandIds.h"
#include "CommandRegister.h"
#include "IUiDocumentViewManager.h"
#include "IUiDocument.h"
#include "IDocument.h"
#include "UiCommonDialog.h"
#include "UiDocumentViewUtils.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 


using namespace gcmp;
using namespace Sample;

REGISTER_COMMAND(CmdCloseFile)

CmdCloseFile::CmdCloseFile()
    :CommandBase(ID_CMD_CLOSE_FILE, true)
{
}

bool CmdCloseFile::IsEnabled() const
{
    return UiDocumentViewUtils::GetCurrentDocument() != nullptr;
}

bool CmdCloseFile::IsVisible() const
{
    return UiDocumentViewUtils::GetCurrentDocument() != nullptr;
}

gcmp::OwnerPtr<gcmp::IAction> CmdCloseFile::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IUiDocumentViewManager* pUiDocViewMgr = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocViewMgr, L"获取UI文档视图管理器失败", L"GDMP", L"2023-09-30");
    IUiDocument* pUiDoc = pUiDocViewMgr->GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDoc, L"获取UI文档失败", L"GDMP", L"2023-09-30");
    IDocument* pDoc = pUiDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"获取文档失败", L"GDMP", L"2023-09-30");
    if (pDoc->IsChangedAfterPreviousSave())
    {
        std::wstring docName = pDoc->GetName();
        std::wstring message = L"未保存，是否仍然关闭“" + docName + L"”文件？";
        if (UiCommonDialog::ButtonType::Yes == UiCommonDialog::ShowMessageBox(GBMP_TR(L"关闭文件"), message, (int)UiCommonDialog::ButtonType::Yes | (int)UiCommonDialog::ButtonType::No))
        {
            pUiDocViewMgr->CloseDocument(pUiDoc->GetId());
        }
    }
    else
    {
        pUiDocViewMgr->CloseDocument(pUiDoc->GetId());
    }

    return nullptr;
}
